﻿package ascript.components.animation
{
	//#########################
	// Header Includes
	//#########################
	import ascript.Game;
	import ascript.utilities.PointSimple;
	import ascript.GameComponent;
	import ascript.GameLevel;
	import ascript.GameObject;
	import Box2D.Common.Math.b2Vec2;
	
	//#########################
	// GeneralModifiers Class
	//#########################
	public class AnimationCharacter extends GameComponent
	{
		//=========================
		// Constructors
		//=========================	
		public function AnimationCharacter():void
		{
		}
		
		//=========================
		// Members
		//=========================
		override public function initComponent( tGame:Game, tObject:GameObject ):void
		{
			// Set Default Attributes
			defaultAttribute( tObject, "AnimationCharacterIsEnabled", true );
			defaultAttribute( tObject, "AnimationMovieName", "Graphic" );
			defaultAttribute( tObject, "AnimationIndex", 0 );
			defaultAttribute( tObject, "Direction", new b2Vec2() );
		}
		
		override public function applyAttribute( tGame:Game, tObject:GameObject, tAttribute:String ):void
		{
			switch( tAttribute )
			{
				case "AnimationIndex":
					if ( tObject.getAttribute( tObject.getAttribute( "AnimationMovieName" ) ) != null )
					{
						( tObject.getAttribute( tObject.getAttribute( "AnimationMovieName" ) ) ).setGraphic( tObject.getAttribute( "AnimationIndex" ) );
					}
					break;
				default:
					super.applyAttribute( tGame, tObject, tAttribute );
					break;
			}
		}
		
		override public function updateComponent( tGame:Game, tObject:GameObject, tLevel:GameLevel ):void
		{
			if ( tObject.getAttribute( "AnimationCharacterIsEnabled" ) )
			{
				var tempDirection:b2Vec2 = ( tObject.getAttribute( "Direction" ) ).Copy();
				
				if ( ( tempDirection.x != 0 && Math.abs( tempDirection.x ) > 0 ) || ( tempDirection.y != 0 && Math.abs( tempDirection.y ) > 0 ) )
				{
					var tempAnimationIndex:uint = tObject.getAttribute( "AnimationIndex" );
					
					// Set character movement walks		
					if ( Math.abs( tempDirection.x ) >= Math.abs( tempDirection.y ) )
					{
						tempAnimationIndex = 0;
						
						// Set X Anims (Left, Right)
						if ( tempDirection.x < 0 )
						{
							tObject.scaleX = -1;
						}
						else
						{
							tObject.scaleX = 1;
						}
					}
					else
					{
						// Set Y Anims (Top, Down)
						if ( tempDirection.y < 0 )
						{
							tempAnimationIndex = 1;
						}
						else
						{
							tempAnimationIndex = 2;
						}
					}
					
					// Apply Animation
					tObject.setAttribute( "AnimationIndex", tempAnimationIndex );
				}
			}
		}
	}
}